package com.twu.biblioteca.menu;



/* This class understands how to interact with the user.
        */

import com.twu.biblioteca.BibliotecaApp;
import com.twu.biblioteca.User;
import com.twu.biblioteca.menuitems.CheckMembershipDetailsMenuItem;
import com.twu.biblioteca.menuitems.DisplayAvailableBooksMenuItem;
import com.twu.biblioteca.menuitems.ReserveABookMenuItem;
import com.twu.biblioteca.menuitems.ViewMoviesMenuItem;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;

public class Menu {
    static ArrayList<MenuItem> menu;
    private static PrintStream printStream;
    private static InputStream inputStream;
    String errorMessageForInvalidOptionSelection;


    public Menu(PrintStream printStream1, InputStream inputStream1) {
        menu = new ArrayList<MenuItem>();
        printStream = printStream1;
        inputStream = inputStream1;
        errorMessageForInvalidOptionSelection = "Select a valid option!!";
    }

    public static void println(Object message) {
        printStream.println(message);
    }

    public static void print(Object message) {
        printStream.print(message);
    }

    public static String getInput() {
        Scanner scanner = new Scanner(inputStream).useDelimiter(System.getProperty("line.separator"));
        return scanner.next();
    }

    public void run() {
        create();
        displayMenuOptions();
        runOption(selectMenuOption());
    }

    public void addMenuItem(MenuItem menuItem) {
        menu.add(menuItem);
    }

    public void create() {
        addMenuItem(new DisplayAvailableBooksMenuItem(BibliotecaApp.bibliotecaLibrary));
        addMenuItem(new ReserveABookMenuItem(BibliotecaApp.bibliotecaLibrary));
        addMenuItem(new ViewMoviesMenuItem(BibliotecaApp.bibliotecaLibrary));
        addMenuItem(new CheckMembershipDetailsMenuItem());
    }

    public void displayMenuOptions() {
        for (MenuItem menuItem : menu) {
            println(menuItem);
        }
    }

    public String selectMenuOption() {
        Scanner scanner = new Scanner(inputStream).useDelimiter(System.getProperty("line.separator"));
        println("Enter the number next to the option you wish to select and press ENTER.");
        return scanner.next();
    }

    public boolean optionIsValid(String selectedOption) {
        return menuItemObjectAssociatedWithSelectedLabel(selectedOption) != null;
    }

    private MenuItem menuItemObjectAssociatedWithSelectedLabel(String selectedOption) {
        for (MenuItem menuItem : menu) {
            if (menuItem.optionLabel.equals(selectedOption)) {
                return menuItem;
            }
        }
        return null;
    }

    public void runOption(String selectedOption) {
        if (optionIsValid(selectedOption)) {
            menuItemObjectAssociatedWithSelectedLabel(selectedOption).runOption();
        } else {
            println(errorMessageForInvalidOptionSelection);
        }
    }

    public static User login() {
        println("Username: ");
        String userName = Menu.getInput();

        println("Password: ");
        String password = Menu.getInput();

        return new User().login(userName, password);
    }
}